$(function(){

	var BASE_URL = $("base").attr('href');
	var $modal = $("#my-alert");
	window.alert = function(content , title){
		
		title = ((typeof title) == 'undefined' ? '提示信息' : title);
		$("#my-alert .am-modal-hd").html(title);
		content = ((typeof content) == 'undefined' ? '(ーー゛) 你要弹什么？' : content);
		$("#my-alert .am-modal-bd").html(content);
		$modal.modal();
	}	

	var $confirm = $("#my-confirm");
	
	window.confirm = function(content, title) {
		title = ((typeof title) == 'undefined' ? '选择信息' : title);
		$("#my-confirm .am-modal-hd").html(title);
		content = ((typeof content) == 'undefined' ? '(ーー゛) 你要弹什么？' : content);
		$("#my-confirm .am-modal-bd").html(content);
		$confirm.modal();

	}
	
	$("#btn-submit").click(function(){

		$text = $.trim( $("#text").val());

		if($text == '') {
			alert("文本不能为空");
			return false;
		}

		$.ajax({
			type:'post',
			url: BASE_URL + 'home/audio',
			data: $.param({text: $text}),
			dataType: 'json',
			success: function(data){
				console.log(data);
				if(data.err_no == 0)
				{
					$("#bd-audio").attr('src', data.results['audio']);

					$("#icon").removeClass('am-icon-volume-down');
					$("#icon").addClass('am-icon-volume-up');
					$audio = $("#bd-audio")[0];
					$audio.play();  
				}
				else
				{
					alert(data.err_msg);
				}
			}
		});
	});

	$("#bd-audio").on("ended",function(){

		$("#icon").removeClass('am-icon-volume-up');
		$("#icon").addClass('am-icon-volume-off');
		$("#bd-audio").data('state', 0);
	});

	$("#btn-audio").click(function(){

		if($("#bd-audio").attr('src') == '')
		{
			alert("没有可播放内容");
			return false;
		}

		$audio = $("#bd-audio")[0];

		if($("#bd-audio").data('state') == 1) {
			$audio.pause(); //am-icon-volume-down
			$("#icon").removeClass('am-icon-volume-up');
			$("#icon").addClass('am-icon-volume-off');
			$("#bd-audio").data('state', 0);
		}
		else
		{
			$audio.play();
			$("#icon").removeClass('am-icon-volume-off');
			$("#icon").addClass('am-icon-volume-up');
			$("#bd-audio").data('state', 1);
		}		
	});
});